package com.waimai.demo.Controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.waimai.demo.Service.EmployeeService;
import com.waimai.demo.back.R;
import com.waimai.demo.domain.Employee;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;

    /**
     * 登陆校验
     *
     * @param rep
     * @param employee
     * @return
     */
    @PostMapping("/login")
    public R login(HttpServletRequest rep, @RequestBody Employee employee) {

        //利用md5加密
        String password = DigestUtils.md5DigestAsHex(employee.getPassword().getBytes());
        //使用用户名查询数据库
        LambdaQueryWrapper<Employee> lmq = new LambdaQueryWrapper();
        lmq.eq(Employee::getUsername, employee.getUsername());
        Employee emp = employeeService.getOne(lmq);
        if (emp == null) {
            return R.error("登陆失败");
        }
        if (!emp.getPassword().equals(password)) {
            return R.error("登陆失败");
        }
        if (emp.getStatus() == 0) {
            return R.error("您的账号已禁用");
        }
        rep.getSession().setAttribute("employee", emp.getId());
        return R.success(emp);
    }

    /**
     * 退出功能
     *
     * @param req
     * @return
     */
    @PostMapping("logout")
    public R logout(HttpServletRequest req) {
        req.getSession().removeAttribute("employee");
        return R.success("退出成功");
    }

    /**
     * 添加员工
     *
     * @param employee
     * @return
     */
    @PostMapping
    public R<String> sava(@RequestBody Employee employee, HttpServletRequest req) {
        log.info("新增员工：{}" + employee.toString());
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        employee.setCreateTime(LocalDateTime.now());
        employee.setUpdateTime(LocalDateTime.now());
        Long id = (Long) req.getSession().getAttribute("employee");
        employee.setCreateUser(id);
        employee.setUpdateUser(id);
        employeeService.save(employee);

        return R.success("新增员工成功");
    }

    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, String name) {
        Page page1 = new Page(page, pageSize);
        LambdaQueryWrapper<Employee> lmq = new LambdaQueryWrapper();
        if (name != null) {
            lmq.like(Employee::getName, name);
        }
        lmq.orderByDesc(Employee::getCreateTime);
        employeeService.page(page1, lmq);
        return R.success(page1);
    }

    @GetMapping("/{id}")
    public R<Employee> sava(@PathVariable Long id) {
        System.out.println(id);
        return R.success(employeeService.getById(id));
    }

    @PutMapping
    public R<String> update(@RequestBody Employee employee, HttpServletRequest req) {
        if (employee != null) {
            Long id = (Long) req.getSession().getAttribute("employee");
            employee.setCreateUser(id);
            employee.setUpdateUser(id);
            employeeService.updateById(employee);
            return R.success("修改成功");
        }
        return R.error("您未改动");
    }


}
